<?php

require_once ('common.php');
checkLogin();
$id = trim($_REQUEST['id']);
$type = trim($_REQUEST['type']);
$action = trim($_REQUEST['action']);
if (empty($id) || empty($type) || empty($action) || $action != 'edit')
{
    header('location: ' . APP_URL);
    exit;
}
$pagetitle = 'แก้ไข';
if ($type == 'topic')
{
    $pagetitle .= 'กระทู้';
    $topics = $db->getRow('SELECT * FROM bbs_topics WHERE topic_id = %1', array($id));
    $canEdit = $topics['topic_poster'] == $uid || $user['is_admin'];
    if (!$topics || !$canEdit)
    {
        header('location: ' . APP_URL);
        exit;
    }
} elseif ($type == 'post')
{
    $pagetitle .= 'ความคิดเห็น';
    $posts = getPost($id);
    $canEdit = $posts['poster_id'] == $uid || $user['is_admin'];
    if (!$posts || !$canEdit)
    {
        header('location: ' . APP_URL);
        exit;
    }
}
else
{
    header('location: ' . APP_URL);
    exit;
}
$emos = $bbcode->getEmos();
$icons = getIcons();
$smarty->assign('pagetitle', $pagetitle);
$smarty->display('header.html');
$smarty->assign('uid', $uid);
$smarty->assign('type', $type);
if ($type == 'topic')
{
    $smarty->assign('topics', $topics);
}
if ($type == 'post')
{
    $smarty->assign('posts', $posts);
}
$smarty->assign('emos', $emos);
$smarty->assign('icons', $icons);
$smarty->display('edit.html');
require_once ('sidebar.php');
require_once ('footer.php');

?>